//
// Created by dreamli on 2025/11/7.
//
#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"
#include "database.h"
#include "foods_message_control.h"
#define FOOD_COUNT 18
//插入食物信息

int foods_message_insert(){
    // 1. 补全占位符为6个，与字段数匹配
    const char * sql="insert into foods_message ("
                     "foods_id, foods_name, price, category_id"
                     ") VALUES (?, ?, ?, ?);";

    sqlite3_stmt * stmt;
#define FOOD_COUNT 18
    int rc= sqlite3_prepare_v2(db,sql,-1,&stmt,NULL);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"foods_message_insert预编译出错了：%s",sqlite3_errmsg(db));
        return -1;
    }

    // 2. 固定长度数组，编译正常
    const char * foods[FOOD_COUNT][6] = {
            {"1",   "原味羊肉串", "6.5", "1"},
            {"2",   "秘制牛肉串", "7.0", "1"},
            {"3",   "奥尔良鸡翅", "8.0", "1"},
            {"4",   "炭烤生蚝",   "12.0","2"},
            {"5",   "烤鱿鱼须",   "9.0", "2"},
            {"6",   "烤茄子",     "10.0","3"},
            {"7",   "烤金针菇",   "6.0", "3"},
            {"8",   "烤馒头片",   "3.0", "4"},
            {"9",   "锡纸娃娃菜", "12.0","4"},
            {"10",  "烤羊腰",    "15.0", "5"},
            {"11", "烤鸡胗",    "5.0",  "5"},
            {"12", "烤猪脑",    "18.0", "5"},
            {"13", "烤土豆片",  "4.0", "6"},
            {"14", "烤豆皮卷",  "3.0", "6"},
            {"15", "烤蚕蛹",    "8.0", "6"},
            {"16", "冰镇啤酒",  "6.0", "7"},
            {"17", "酸梅汤",    "5.0", "7"},
            {"18", "可乐",      "4.0", "7"}
    };

    for (int i = 0; i < FOOD_COUNT; i++) {
        // 3. 字符串转整数/浮点数，匹配绑定函数要求
        sqlite3_bind_int(stmt, 1, atoi(foods[i][0]));
        sqlite3_bind_text(stmt, 2, foods[i][1], -1, SQLITE_TRANSIENT);
        sqlite3_bind_double(stmt, 3, atof(foods[i][2]));
        sqlite3_bind_int(stmt, 4, atoi(foods[i][3]));
        rc = sqlite3_step(stmt);
        if (rc != SQLITE_DONE) {
            fprintf(stderr, "第 %d 条插入失败：%s\t\n", i+1, sqlite3_errmsg(db));
            sqlite3_reset(stmt);
            continue;
        }

        printf("第 %d 条插入成功：%s\t\n", i+1, foods[i][1]);
        sqlite3_reset(stmt);
    }

    sqlite3_finalize(stmt);
#undef FOOD_COUNT // 取消宏定义（可选，避免污染全局）
    return 0;
}
//添加食物信息
int foods_message_add(foods_messages * message){
    char sql[256];
    sprintf(sql,"insert into foods_message(foods_name,price,category_id) values('%s',%f,%d)",
            message->foods_name,message->price,message->category_id);
    char * errmsg;
    int rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"食品信息添加出错了:%s\n",errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
    printf("食品信息添加成功!\n");
    return 0;
}

//更新食物信息
int food_message_update(foods_messages * pmessage){
    char sql[256];
    sprintf(sql,
            "update foods_message set foods_name='%s',price='%f',"
            "category_id=%d  where foods_id = %d; ",
            pmessage->foods_name,pmessage->price,pmessage->category_id);
    char * errmsg;
    int rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"食品信息修改出错了: %s\n",errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
    printf("食品信息修改成功!\n");
    return 0;
}

//删除食物信息
int foods_message_delete(int id){
    char sql[256];
    sprintf(sql,
            "delete from foods_message where foods_id = %d; ",
            id);
    char * errmsg;
    int rc = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"食品信息删除出错了: %s\n",errmsg);
        sqlite3_free(errmsg);
        return -1;
    }
    printf("食品信息删除成功!\n");
    return 0;
}

int foods_message_callback(void *data,int argc,char **argv,char **arg_col_name){
    (*(int *)data)++;
    static int row=0;
    if(row==0){
        for(int i=0;i<argc;i++){
            printf("|%s  ",arg_col_name[i]);
        }
        row=1;
        printf("\n");
        printf("---------------------------------------------------------\n");
    }
    for(int i=0;i<argc;i++ ){
        printf("  |  %s",argv[i]);
    }
    printf("|\n");
    return 0;
}
//查询食物信息
int foods_message_query(){
    char * errmsg;
    int rowcount=0;
    char * sql="select foods_id,foods_name,price,category_id from foods_message";
    int rc= sqlite3_exec(db,sql,foods_message_callback,&rowcount,&errmsg);
    if(rc!=SQLITE_OK){
        fprintf(stderr,"食品信息查询失败：%s",errmsg);
        return -1;
    }
    printf("食品信息查询成功！\n");
    return 0;
}
